package com.rex.realm;

import org.apache.shiro.authc.*;
import org.apache.shiro.realm.Realm;

/**
 * Created by frt on 2017/11/10.
 *
 */
public class FirstRealm implements Realm {
    @Override
    public String getName() {
        return "singleRealm";
    }

    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof UsernamePasswordToken;
    }

    @Override
    public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //获取用户名
        String userName = (String)token.getPrincipal();
        //获取密码
        String password = new String((char[])token.getCredentials());

        if(!"Rex".equals(userName)){
            throw new UnknownAccountException();
        }

        if(!"test123".equals(password)){
            throw new IncorrectCredentialsException();
        }

        return new SimpleAuthenticationInfo(userName, password, getName());
    }
}
